package com.eurabia.eggchat.VO;

import com.github.pagehelper.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ResultVO
 * 定义返回数据结构
 */
@Data
public class ResultVO implements Serializable {
    private static final long serialVersionUID = 1L;

    private String code = "0"; // code
    private String msg = ""; // msg
    private long count; // count
    private List data; // data

    public ResultVO() {
    }


    public ResultVO(List data) {
        if (data instanceof Page) {
            Page page = (Page) data;
            this.count = page.getTotal();
        } else {
            this.count = data.size();
        }
        this.data = data;
    }

    public ResultVO(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultVO(String code, String msg, List data) {
        this.code = code;
        this.msg = msg;
        if (data instanceof Page) {
            Page page = (Page) data;
            this.count = page.getTotal();
        } else {
            this.count = data.size();
        }
        this.data = data;
    }

    public ResultVO(String code, String msg, long count, List data) {
        this.code = code;
        this.msg = msg;
        if (data instanceof Page) {
            Page page = (Page) data;
            this.count = page.getTotal();
        } else {
            this.count = data.size();
        }
        this.data = data;
    }

    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("code", code);
        result.put("msg", msg);
        result.put("data", data);
        result.put("count",count);
        return result;
    }

    public static ResultVO fail(String msg) {
        ResultVO jsonData = new ResultVO();
        jsonData.code = "1";
        jsonData.msg = msg;
        return jsonData;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }
}